<template>
  <MyLoading v-if="loadingTag" />
  <div v-else class="films" ref="filmsBox">
    <div class="item" v-for="(item, index) in list" :key="'film' + index" @click="gotoDetial(item.filmId)">
      <div class="left">
        <img :src="item.poster" />
      </div>
      <div class="middle">
        <div>{{ item.name }}</div>
        <div v-if="type === 0">
          <span>观众评分</span>
          <span>{{ item.grade }}</span>
        </div>
        <div>主演：{{ parseActors(item.actors) }}</div>
        <div>{{ item.nation }} | {{ item.runtime }} 分钟</div>
      </div>
      <div class="right">
        <span v-if="type === 0">购票</span>
        <span v-else>预约</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 当我们初始化的时候传递空数组展示，有数据的时候关闭
import { ref, defineProps, PropType, onMounted, defineEmits } from "vue";
import { useRouter } from "vue-router";
import MyLoading from "../components/MyLoading.vue";
// 导入电影列表的相关关键属性数据
import { ListType, ActorType } from "../types/index";

let loadingTag = ref<boolean>(true);
let filmsBox = ref<HTMLElement>();

// 子组件接收父组件中 命名 可以进行修改成驼峰命名
const emit = defineEmits(['getMoreData']);
// 接收我们 props 的值
const props = defineProps({
  list: {
    type: Array as PropType<ListType[]>,
    default: () => [],
  },
  type: {
    type: Number,
    default: () => 0,
  },
});
// 跳转到电影详情
const router = useRouter()
const gotoDetial = (id: number) => {
  // 进行路由跳转
  router.push("/detial/" + id)
};

const debounce = (func: any, delay: any) => {
  let timer: any = null;
  return function () {
    if (timer) {
      clearTimeout(timer);
      timer = null;
    }
    timer = setTimeout(() => {
      func(...arguments);
    }, delay);
  };
}

// 节流防抖
onMounted(() => {

  window.onscroll = debounce(() => {
    const fbx = filmsBox.value as HTMLElement
    if (window.pageYOffset + 50 + window.innerHeight > fbx.scrollHeight) {
      // 通知父页面请求 list 数据
      emit("getMoreData")
    }
  }, 300)
})


console.log(props.list);
if (props.list.length > 0) loadingTag.value = false;

const parseActors = (value: ActorType[]) => {
  let actors = "";
  value.forEach((element: { name: string }) => {
    actors += element.name + " ";
  });
  return actors;
};
</script>

<style scoped lang="scss">
.films {
  padding-bottom: 60px;
  padding-top: 0;

  .item {
    margin-top: 15px;
    padding-bottom: 10px;
    padding-top: 10px;
    display: flex;
    color: #797d82;
    font-size: 13px;
    border-bottom: 1px solid #e7d8d8;
    background-color: #fff;

    .left {
      width: 77px;
      height: 100px;
      margin-left: 20px;

      img {
        width: 66px;
        height: 100%;
      }
    }

    .middle {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      width: 55%;

      div:nth-of-type(1) {
        color: #191a1b;
        font-size: 16px;
      }

      div:nth-of-type(3) {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }

    .right {
      width: 15%;
      display: flex;
      align-items: center;
      margin-right: 20px;

      span {
        border: 1px solid #ff5f16;
        background: white;
        color: #ff5f16;
        border-radius: 2px;
        height: 25px;
        line-height: 25px;
        font-size: 13px;
        width: 50px;
        text-align: center;
      }
    }
  }
}
</style>
